<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 账号管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>账号信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="请输入姓名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="adminId" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="adminIdentify" label="账号"></el-table-column>
                <el-table-column prop="adminName" label="姓名"></el-table-column>
                <el-table-column prop="role" label="类型" :formatter="measure"></el-table-column>
                <el-table-column prop="adminPassword" label="密码"></el-table-column>
                <el-table-column prop="createTime" label="添加时间"></el-table-column>
                <el-table-column prop="updateTime" label="修改时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">修改
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.current"
                    :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="修改" v-model="editVisible" width="30%">
            <el-form label-width="70px">
                <el-form-item label="编号" prop="adminId">
                    <el-input v-model="form.adminId" disabled></el-input>
                </el-form-item>
                <el-form-item label="账号" prop="adminIdentify">
                    <el-input v-model="form.adminIdentify" disabled></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="adminName">
                    <el-input v-model="form.adminName"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="role">
                    <el-select v-model="form.role" placeholder="请选择类型">
                        <el-option key="1" label="医生" :value="1" ></el-option>
                        <el-option key="2" label="药品采购员" :value="2"></el-option>
                        <el-option key="0" label="超级管理员" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="新密码" prop="newPassword">
                    <el-input v-model="form.newPassword" type="password"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="pwd">
                    <el-input v-model="form.pwd" type="password"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { onActivated, ref, reactive, inject, computed } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getAdminList, modifyAdmin, deleteAdmin } from "../../api";

export default {
    name: "admin",
    setup() {
        const query = reactive({
            current: 1,
            pageSize: 10,
            total: 0,
            name: '',
        });
        const tableData = ref([]);
        const pageTotal = ref(0);
        const reload  = inject("reload");
        // let tableData = [];
        // 获取表格数据
        const getData = () => {
            getAdminList(query).then((res) => {
                tableData.value = res.data.message;
                query.total = res.data.total;
            });
        };

        // 查询操作
        const handleSearch = () => {
            query.current = 1;
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.current = val;
            getData();
        };

        // 删除操作
        const handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
                type: "warning",
            }).then(() => {
                deleteAdmin(row.adminId).then(res => {
                    if (res.code === 200) {
                        reload();
                        ElMessage.success('删除成功');
                    } else {
                        console.log("[删除账号] 失败: " + res.message);
                        ElMessage.error('服务器繁忙，请稍后重试');
                    }
                })
            }).catch(() => {});
        };

        const measure = (row, column, cellValue, index) => {
            console.log(cellValue);
            if (cellValue === 1 || cellValue === '1') {
                return '医生';
            } else if (cellValue === 2 || cellValue === '2') {
                return "药品采购员";
            } else {
                return "超级管理员";
            }
        }

        // 表格编辑时弹窗和保存
        const editVisible = ref(false);
        let form = reactive({
            adminId: null,
            adminIdentify: '',
            adminName: '',
            adminPassword: '',
            newPassword: '',
            role: '',
            pwd: ''
        });
        const handleEdit = (index, row) => {
            Object.keys(form).forEach((item) => {
                form[item] = row[item];
            });
            // form['role'] = String(form['role']);
            editVisible.value = true;
        };
        const saveEdit = () => {
            if (form.newPassword !== form.pwd) {
                ElMessage.error('新密码与确认密码不一致');
            } else {
                form.adminPassword = form.pwd;
                modifyAdmin(form).then(res => {
                    if (res.code === 200) {
                        reload();
                        ElMessage.success('修改成功');
                        editVisible.value = false;
                    } else {
                        console.log("[修改人员] 失败: " + res.message);
                        ElMessage.error('服务器繁忙，请稍后重试');
                    }
                });
            }
        };

        onActivated(() => {
            console.log("Admin onActivated!");
            getData();
        });

        return {
            query,
            tableData,
            pageTotal,
            editVisible,
            form,
            measure,
            handleSearch,
            handlePageChange,
            handleDelete,
            handleEdit,
            saveEdit,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
